{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5aaabf67",
   "metadata": {},
   "source": [
    "---------\n",
    "* 2022-10-11 Week08\n",
    "* Python课堂笔记\n",
    "---------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7ae847d",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 0.代码的注释  \n",
    "> 1.用户输入  \n",
    "> 2.格式化输出  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b27a5bb2",
   "metadata": {},
   "source": [
    "# 0.代码的注释\n",
    ">1.#:一般写在代码的最上边，或者每一行代码的最后，用于代码思路的记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d520190",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Author:Joshua"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89c441a0",
   "metadata": {},
   "source": [
    "> 2.'''注释内容‘’‘，一般注释代码块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69e3a2d4",
   "metadata": {},
   "source": [
    "## 1.用户输入\n",
    ">1.input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "afe91bf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:Joshua\n",
      "password:123456\n",
      "Joshua\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统\n",
    "username=input(\"username:\")\n",
    "password=input(\"password:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "462127e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:aaoo\n",
      "密码:oooo\n",
      "账号不存在或密码不正确！\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代1； 增加判断方法\n",
    "#假设我们数据库有用户信息：账号：JOshua 密码：123456\n",
    "user1 = \"Joshua\"\n",
    "password1 =\"123456\"\n",
    "\n",
    "username=input(\"账号:\")\n",
    "password=input(\"密码:\")\n",
    "\n",
    "#情景一：输入信息正确，登陆成功 \n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+ user1 +\"登录!\")\n",
    "#情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e9885743",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ppp\n",
      "密码:lll\n",
      "账号不存在或密码不正确！\n",
      "您还有2次尝试的机会\n",
      "账号:pppp\n",
      "密码:llll\n",
      "账号不存在或密码不正确！\n",
      "您还有1次尝试的机会\n",
      "账号:Joshua\n",
      "密码:123456\n",
      "欢迎Joshua登录!\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2: 增加循环方法\n",
    "#假设我们数据库有用户信息：账号：JOshua 密码：123456\n",
    "user1 = \"Joshua\"\n",
    "password1 =\"123456\"\n",
    "\n",
    "for i in range(3,0,-1): #i是一个int类型\n",
    "    username=input(\"账号:\")\n",
    "    password=input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息正确，登陆成功 \n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+ user1 +\"登录!\")\n",
    "        break #终止循环\n",
    "    #情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请一小时后再来尝试\")\n",
    "        print(\"账号不存在或密码不正确！\")\n",
    "        print(\"您还有\" + str(i-1) +\"次尝试的机会\")  #str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0140bfc8",
   "metadata": {},
   "source": [
    "## 2.格式化输出\n",
    "> 1.字符串拼接  \n",
    ">  2.占位符  \n",
    ">  3.format()  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0ef7ce4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:Joshua\n",
      "年龄:28\n",
      "工作:star\n",
      "收入:1亿\n",
      "地区:美国\n",
      "-----INFO OFJoshua------\n",
      "年龄:28\n",
      "工作:star\n",
      "收入:1亿\n",
      "地区:美国\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统  方法一：字符串拼接\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "#三个引号变成格式化\n",
    "info = '''-----INFO OF''' +name+'''------'''+''' \n",
    "年龄:'''+age+'''\n",
    "工作:'''+job+'''\n",
    "收入:'''+salary+'''\n",
    "地区:'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f0a6f6b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄:99\n",
      "99 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄:\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "df0f7a86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:Joshua\n",
      "年龄:28\n",
      "工作:star\n",
      "收入:100000000\n",
      "地区:America\n",
      "-----INFO OF Joshua-------\n",
      "年龄:28\n",
      "工作:star\n",
      "收入:100000000\n",
      "地区:America\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s:string字符串,依次有序输入  %d:number  %:float\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄:\"))  #强制将str转化成int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''-----INFO OF %s-------\n",
    "年龄:%d\n",
    "工作:%s\n",
    "收入:%s\n",
    "地区:%s'''%(name,age,job,salary,reg)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dc883026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:Joshua\n",
      "年龄:29\n",
      "工作:star\n",
      "收入:10000000\n",
      "地区:Ameria\n",
      "-----INFO OF 29-------\n",
      "年龄:29\n",
      "工作:star\n",
      "收入:10000000\n",
      "地区:Ameria\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format()格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名:\")\n",
    "age = int(input(\"年龄:\"))  #强制将str转化成int\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''-----INFO OF {年龄}-------\n",
    "年龄:{年龄}\n",
    "工作:{工作}\n",
    "收入:{收入}\n",
    "地区:{地区}'''.format(年龄=age,工作=job,收入=salary,地区=reg) #不用考虑顺序\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "751f14f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业上课检查\n",
    "#要求\n",
    "#运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "#让用户输入商品编号进行商品的购买。\n",
    "#用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "#用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "\n",
    "Commodity =[ ['Iphone14',5000],\n",
    "      ['Iphone pro14',8999],\n",
    "      ['Macbook', 10000]]\n",
    "\n",
    "\n",
    "balance = int(input(\"支付宝余额:\"))\n",
    "Shopping_cart =[]\n",
    "\n",
    "print(Commodity)\n",
    "\n",
    "#循环退出条件，为1时退出while循环\n",
    "quit = 0\n",
    "\n",
    "while(quit!=1):\n",
    "    print(\"----购物车小程序----\")\n",
    "    if len(commondity) < 1:\n",
    "        print(\"商品已售空，程序退出\")\n",
    "    break\n",
    "    \n",
    "for index,item in enumerate(commondity):\n",
    "    print(index+1,item)\n",
    "  \n",
    " #number商品编号，默认为0，number=0\n",
    "#price为商品价格，默认为0，price=0\n",
    "\n",
    "for index,item in enumerate (commodity):\n",
    "    number=int(input(\"输入你要买的商品编号，输入-1则退出\"))\n",
    "    if number == -1:\n",
    "        quit = 1\n",
    "    break\n",
    "\n",
    "if number <= len(commodity) and len(commodity) >= 1:\n",
    "    number = number-1\n",
    "    #commodity_choose为已选择的商品\n",
    "    print(\"已选择商品:\"%commodity)\n",
    "    price = commodity[number][1]\n",
    "    \n",
    "#商品购物处理\n",
    "\n",
    "if balance >= price:\n",
    "    balance = balance - price\n",
    "    #commodity_choose为选择的商品\n",
    "    commodity_choose = commodity.pop(number)\n",
    "    cart.append(commodity_choose)\n",
    "    print(\"购买成功，购买了%s，花费%d，您的剩余余额为:%d‘%commodity_choose[0],price,balance\")    \n",
    "\n",
    "    else:\n",
    "        print(\"余额不足，无法消费，请重新选择\")    \n",
    "        break\n",
    "\n",
    "else:\n",
    "     print(\"商品已售空或商品编号超出范围，取消购物\")\n",
    "        \n",
    "break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c0d892",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
